#ifndef MAP_DOWNLOAD_ACTION_SERVER_H
#define MAP_DOWNLOAD_ACTION_SERVER_H

#include "rclcpp/rclcpp.hpp"
#include "rclcpp_action/rclcpp_action.hpp"
#include "rms_pkg/action/map_download.hpp"
#include <QCoreApplication>
#include <QObject>
#include <QTimer>
#include <memory>
#include <QMutex>
#include "../include/seer/SCStatusTcp.h"
#include "../include/seer/RequestManager.h"
#include "../include/global_logger.h"
#include <QEvent>
#include <QCoreApplication>

using namespace std::chrono_literals;

// 自定义事件类型用于处理execute调用
class ExecuteGoalEvent : public QEvent {
public:
    static const QEvent::Type Type = static_cast<QEvent::Type>(QEvent::User + 1);
    
    explicit ExecuteGoalEvent(std::shared_ptr<rclcpp_action::ServerGoalHandle<rms_pkg::action::MapDownload>> goal_handle)
        : QEvent(Type), goal_handle_(goal_handle) {}
    
    std::shared_ptr<rclcpp_action::ServerGoalHandle<rms_pkg::action::MapDownload>> goal_handle() const {
        return goal_handle_;
    }

private:
    std::shared_ptr<rclcpp_action::ServerGoalHandle<rms_pkg::action::MapDownload>> goal_handle_;
};

// 添加上下文结构体定义
struct MapDownloadContext {
    std::shared_ptr<rclcpp_action::ServerGoalHandle<rms_pkg::action::MapDownload>> goal_handle;
    QString file_path;
    quint32 chunks_sent;
    quint32 total_chunks;
    std::shared_ptr<rms_pkg::action::MapDownload::Result> result;
    std::shared_ptr<rms_pkg::action::MapDownload::Feedback> feedback;
};

class MapDownloadActionServer : public QObject, public rclcpp::Node
{
    Q_OBJECT

public:
    using MapDownload = rms_pkg::action::MapDownload;
    using GoalHandleMapDownload = rclcpp_action::ServerGoalHandle<MapDownload>;

    explicit MapDownloadActionServer(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
    
    // 重写event方法处理自定义事件
    bool event(QEvent* event) override;

private:
    rclcpp_action::Server<MapDownload>::SharedPtr action_server_;
    
    // Socket相关
    SCStatusTcp* m_confSocket;
    RequestManager* m_requestManager;
    
    // 连接参数
    QString m_robotIp;
    int m_confPort;
    
    // 连接管理
    QTimer* m_reconnectTimer;
    bool m_wasConnected;
    int m_reconnectAttempts;
    const int MAX_RECONNECT_ATTEMPTS = 10;
    
    // Action相关函数
    rclcpp_action::GoalResponse handle_goal(
        const rclcpp_action::GoalUUID & uuid,
        std::shared_ptr<const MapDownload::Goal> goal);
        
    rclcpp_action::CancelResponse handle_cancel(
        const std::shared_ptr<GoalHandleMapDownload> goal_handle);
        
    void handle_accepted(
        const std::shared_ptr<GoalHandleMapDownload> goal_handle);
        
    void execute(
        const std::shared_ptr<GoalHandleMapDownload> goal_handle);
        
    // 连接管理函数
    void initializeConnections();
    void connectToRobot();
    bool isConnected() const;
    void scheduleReconnect();
    void resetReconnectAttempts();
    void updateConnectionStatus();
    
    // 地图下载核心函数
    void downloadMapStreaming(
        const QString& mapName,
        std::function<void(const QByteArray&, bool)> data_callback);
        
private slots:
    void reconnectTimerTimeout();
    void handleSocketStateChanged(QAbstractSocket::SocketState state);
    void handleSocketError(QAbstractSocket::SocketError error);

// 添加信号声明
signals:
    void downloadFinished(MapDownloadContext* context);
    void downloadFailed(MapDownloadContext* context, const QString& error_msg);
    void downloadCanceled(MapDownloadContext* context);
};

#endif // MAP_DOWNLOAD_ACTION_SERVER_H